package com.work.work.config;

        import com.work.work.interceptor.JwtInterceptor;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.web.servlet.config.annotation.CorsRegistry;
        import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
        import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

        @Configuration
        public class WebConfig implements WebMvcConfigurer {

            private final JwtInterceptor jwtInterceptor;

            @Value("${jwt.enable}")
            private boolean jwtEnable;

            public WebConfig(JwtInterceptor jwtInterceptor) {
                this.jwtInterceptor = jwtInterceptor;
            }

            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOriginPatterns("*")  // 使用 allowedOriginPatterns 代替 allowedOrigins
                        .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
                        .allowedHeaders("*")
                        .exposedHeaders("Authorization")  // 明确暴露 Authorization 头
                        .allowCredentials(true)  // 允许携带凭证
                        .maxAge(3600L);
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                if (jwtEnable) {
                    registry.addInterceptor(jwtInterceptor)
                            .addPathPatterns("/**")
                            .excludePathPatterns(
                                    "/user/login",
                                    "/swagger-ui/**",
                                    "/swagger-ui.html",
                                    "/v3/api-docs/**",
                                    "/swagger-resources/**",
                                    "/webjars/**",
                                    "/error"
                            )
                            .order(1);  // 设置拦截器优先级
                }
            }
        }